package com.wholeintech.knx;

import static com.wholeintech.utils.CommonIntent.ACTION_CTL_AIRCONDITION;
import static com.wholeintech.utils.CommonIntent.ACTION_CTL_CURTAIN;
import static com.wholeintech.utils.CommonIntent.ACTION_CTL_FLOOR_HEATING;
import static com.wholeintech.utils.CommonIntent.ACTION_CTL_FRESHAIR;
import static com.wholeintech.utils.CommonIntent.ACTION_CTL_LIGHT;
import static com.wholeintech.utils.CommonIntent.ACTION_CTL_SWITCH;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.wholeintech.protocol.NCN5120;
import com.wholeintech.entity.KnxDevice;
import com.wholeintech.zigbee.gw.DeviceManager;
import com.wholeintech.utils.CommonUtil;
import com.wholeintech.entity.DeviceDb;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.ArrayList;

import static com.wholeintech.utils.CommonUtil.bytesToHexString;

public class KNXDataProcess {

    private static final int KNX_PATH_NAME_SW_OPEN = 1;
    private static final int KNX_PATH_NAME_SW_CLOSE = 2;
    private static final int KNX_PATH_NAME_SW_UP = 3;
    private static final int KNX_PATH_NAME_SW_DOWN = 4;
    private static final int KNX_PATH_NAME_SW_STOP = 5;
    private static final int KNX_PATH_NAME_SW_FB = 6;
    private static final int KNX_PATH_NAME_BRIGHTNESS = 7;
    private static final int KNX_PATH_NAME_BRIGHTNESS_FB = 8;
    private static final int KNX_PATH_NAME_TEMPE = 9;
    private static final int KNX_PATH_NAME_TEMPE_FB = 10;
    private static final int KNX_PATH_NAME_WIND_AUTO = 12;
    private static final int KNX_PATH_NAME_WIND_MANUAL = 13;
    private static final int KNX_PATH_NAME_WIND0 = 14;
    private static final int KNX_PATH_NAME_WIND1 = 15;
    private static final int KNX_PATH_NAME_WIND2 = 16;
    private static final int KNX_PATH_NAME_WIND3 = 17;
    private static final int KNX_PATH_NAME_WIND4 = 18;
    private static final int KNX_PATH_NAME_WIND5 = 19;
    private static final int KNX_PATH_NAME_WIND6 = 20;
    private static final int KNX_PATH_NAME_WIND7 = 21;
    private static final int KNX_PATH_NAME_WIND_FB = 22;
    private static final int KNX_PATH_NAME_MODE0 = 23;
    private static final int KNX_PATH_NAME_MODE1 = 24;
    private static final int KNX_PATH_NAME_MODE2 = 25;
    private static final int KNX_PATH_NAME_MODE3 = 26;
    private static final int KNX_PATH_NAME_MODE4 = 27;
    private static final int KNX_PATH_NAME_MODE5 = 28;
    private static final int KNX_PATH_NAME_MODE6 = 29;
    private static final int KNX_PATH_NAME_MODE7 = 30;
    private static final int KNX_PATH_NAME_MODE_FB = 31;

    private final static String TAG = "KNXDataProcess";
    private Context mContext = null;

    KnxDevice mKnx;

    NCN5120 mKnxDev = new NCN5120();

    public KNXDataProcess(Context context) {
        this.mContext = context;
    }

    public ArrayList<String> getPackStr(String devid, Intent intent) {
        String Str = "";
        ArrayList<String> list = new ArrayList<String>();
        int cmd = 0;
        int cmdData = 0;
        if (intent.getAction().equals(ACTION_CTL_LIGHT)) {
            int sw = intent.getIntExtra("sw", -1);
            int brightness = intent.getIntExtra("brightness", -1);
            Log.d(TAG, "ACTION_CTL_LIGHT sw:" + sw + "brightness:" + brightness);

            if (sw == 0) {
                cmd = KNX_PATH_NAME_SW_CLOSE;
            } else if (sw >= 1) {
                cmd = KNX_PATH_NAME_SW_OPEN;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }


            if (brightness != -1) {
                cmd = KNX_PATH_NAME_BRIGHTNESS;
                cmdData = brightness;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }

        } else if (intent.getAction().equals(ACTION_CTL_AIRCONDITION)) {
            Log.d(TAG, "ACTION_CTL_AIRCONDITION");
            int sw = intent.getIntExtra("sw", -1);
            int mode = intent.getIntExtra("mode", -1);
            int temp = intent.getIntExtra("temperature", -1);
            int wind = intent.getIntExtra("wind", -1);

            Log.d(TAG, "ACTION_CTL_AIRCONDITION sw :" + sw + "mode:" + mode + "temp:" + temp + "wind:" + wind);
            if (sw == 0) {
                cmd = KNX_PATH_NAME_SW_CLOSE;
            } else if (sw >= 1) {
                cmd = KNX_PATH_NAME_SW_OPEN;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }


            if (temp != -1) {
                cmd = KNX_PATH_NAME_TEMPE;
                cmdData = temp;
                if (cmd != 0){
                    Str = getCmdString(devid, cmd, cmdData);
                    if (Str != null)
                    {
                        list.add(Str);
                    }
                }
            }

            if (mode != -1) {
                cmd = KNX_PATH_NAME_MODE0 + mode - 1;
                cmdData = mode;
                if (cmd != 0){
                    Str = getCmdString(devid, cmd, cmdData);
                    if (Str != null)
                    {
                        list.add(Str);
                    }
                }
            }
            if (wind != -1) {
                cmd = KNX_PATH_NAME_WIND0 + wind - 1;
                cmdData = wind;
                if (cmd != 0){
                    Str = getCmdString(devid, cmd, cmdData);
                    if (Str != null)
                    {
                        list.add(Str);
                    }
                }
            }

        } else if (intent.getAction().equals(ACTION_CTL_FLOOR_HEATING)) {
            int sw = intent.getIntExtra("sw", -1);
            //int mode = intent.getIntExtra("mode", -1);
            int temp = intent.getIntExtra("temperature", -1);
            Log.d(TAG, "ACTION_CTL_FLOOR_HEATING sw :" + sw );
            if (sw == 0) {
                cmd = KNX_PATH_NAME_SW_CLOSE;
            } else if (sw >= 1) {
                cmd = KNX_PATH_NAME_SW_OPEN;
            }

            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }

            if (temp != -1) {
                cmd = KNX_PATH_NAME_TEMPE;
                cmdData = temp;
                if (cmd != 0){
                    Str = getCmdString(devid, cmd, cmdData);
                    if (Str != null)
                    {
                        list.add(Str);
                    }
                }
            }

        } else if (intent.getAction().equals(ACTION_CTL_CURTAIN)) {
            Log.d(TAG, "ACTION_CTL_CURTAIN");
            int runstate = intent.getIntExtra("runstate", -1);
            int channel = intent.getIntExtra("channel", 0);
            if (runstate == 0)
                cmd = KNX_PATH_NAME_SW_STOP;
            else if (runstate == 1) {
                cmd = KNX_PATH_NAME_SW_OPEN;
            } else if (runstate == 2) {
                cmd = KNX_PATH_NAME_SW_CLOSE;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }

        } else if (intent.getAction().equals(ACTION_CTL_FRESHAIR)) {
            int sw = intent.getIntExtra("sw", -1);
            int mode = intent.getIntExtra("mode", -1);
            int wind = intent.getIntExtra("wind", -1);
            //int temp = intent.getIntExtra("temperature", -1);
            Log.d(TAG, "ACTION_CTL_AIRCONDITION sw :" + sw + "mode:" + mode + "wind:" + wind);
            if (sw == 0) {
                cmd = KNX_PATH_NAME_SW_CLOSE;
            } else if (sw >= 1) {
                cmd = KNX_PATH_NAME_SW_OPEN;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }
            /*if (temp != -1) {
                cmd = KNX_PATH_NAME_TEMPE;
                cmdData = temp;
            }*/

            if (mode != -1) {
                cmd = KNX_PATH_NAME_MODE0+mode;
                cmdData = mode;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }

            if (wind != -1){
                cmd = KNX_PATH_NAME_WIND0+wind;
                cmdData = wind;
            }
            if (cmd != 0){
                Str = getCmdString(devid, cmd, cmdData);
                if (Str != null)
                {
                    list.add(Str);
                }
            }

        } else if (intent.getAction().equals(ACTION_CTL_SWITCH)) {
            Log.d(TAG, "ACTION_CTL_SWITCH");

        } else {
            Log.d(TAG, "NONE PROCESS Method");
        }

        return list;
    }

    byte[] getCmdBytes(String devid, int cmd, int cmdData) {
        byte[] out = new byte[10];
        long data = 0;
        DbManager db = CommonUtil.MyDbUtils();
        try {
//			mKnx = db.findFirst(Selector.from(KnxDevice.class).where("devId", "=", devid));
            mKnx = db.selector(KnxDevice.class).where("devId", "=", devid).findFirst();
            if (mKnx != null) {

                data = getDbData(mKnx, cmd);
                if (data == -1) {
                    out[0] = 0;
                    Log.e(TAG, "getCmdBytes:" + "data error："+ data + " cmd:" + cmd + " cmdData:" + cmdData);
                    return out;
                }

                Log.d(TAG, "getCmdBytes:" + "Devid￡o" + mKnx.getDevId() + "data:" + data);

                if (KnxDevice.getBits(data) <= 7) {
                    out = mKnxDev.groupWriteBool(KnxDevice.getMainGroup(data), KnxDevice.getMiddleGroup(data), KnxDevice.getGroup(data), KnxDevice.getValue(data));
                } else if (KnxDevice.getBits(getDbData(mKnx, cmd)) == 8) {
//                    DeviceDb me = db.findFirst(Selector.from(DeviceDb.class).where("devId", "=", devid));
                    DeviceDb me = db.selector(DeviceDb.class).where("devId", "=", devid).findFirst();
                    if (me != null && me.getType() == DeviceManager.TYPE_LIGHT_DIMMER) {
                        //Log.d(TAG, "Box rename: me.getBrightness():" + devid +"getBrightness"+ me.getBrightness() +"cmdData:"+ cmdData);
                        out = mKnxDev.groupWriteByte(KnxDevice.getMainGroup(data), KnxDevice.getMiddleGroup(data), KnxDevice.getGroup(data), cmdData);
                    } else {
                        out = mKnxDev.groupWriteByte(KnxDevice.getMainGroup(data), KnxDevice.getMiddleGroup(data), KnxDevice.getGroup(data), KnxDevice.getValue(data));
                    }

                } else if (KnxDevice.getBits(getDbData(mKnx, cmd)) > 8) {
//                    DeviceDb me = db.findFirst(Selector.from(DeviceDb.class).where("devId", "=", devid));
//                    DeviceDb me = db.selector(DeviceDb.class).where("devId", "=", devid).findFirst();
                    out = mKnxDev.groupWriteFloat(KnxDevice.getMainGroup(data), KnxDevice.getMiddleGroup(data), KnxDevice.getGroup(data), cmdData);
                } else {

                }

            } else {
                Log.d(TAG, "Box rename: Not found, devId:" + devid);
            }
        } catch (DbException | NumberFormatException e) {
            e.printStackTrace();

        }
        return out;
    }

    String getCmdString(String devid, int cmd, int cmdData) {
        String strOut = null;
        byte[] Temp = (getCmdBytes(devid, cmd, cmdData));
        if (Temp[0] == 0)
            return strOut;
        strOut = bytesToHexString(Temp, Temp.length);
        return strOut;
    }

    static public int getCmdSwOpen() {
        return KNX_PATH_NAME_SW_OPEN;
    }

    static public int getCmdSwClose() {
        return KNX_PATH_NAME_SW_CLOSE;
    }

    long getDbData(KnxDevice dev, int cmd) {
        long data = 0;
        switch (cmd) {
            case KNX_PATH_NAME_SW_OPEN:
                data = dev.getSwOpen();
                break;
            case KNX_PATH_NAME_SW_CLOSE:
                data = dev.getSwClose();
                break;
            case KNX_PATH_NAME_SW_UP:
                data = dev.getSwUp();
                break;
            case KNX_PATH_NAME_SW_DOWN:
                data = dev.getSwDown();
                break;
            case KNX_PATH_NAME_SW_STOP:
                data = dev.getSwStop();
                break;
            case KNX_PATH_NAME_SW_FB:
                data = dev.getSwFb();
                break;
            case KNX_PATH_NAME_BRIGHTNESS:
                data = dev.getBrightness();
                break;
            case KNX_PATH_NAME_BRIGHTNESS_FB:
                data = dev.getBrightnessFb();
                break;
            case KNX_PATH_NAME_TEMPE:
                data = dev.getTempe();
                break;
            case KNX_PATH_NAME_TEMPE_FB:
                data = dev.getTempeFb();
                break;
            case KNX_PATH_NAME_WIND_AUTO:
                data = dev.getWindAuto();
                break;
            case KNX_PATH_NAME_WIND_MANUAL:
                data = dev.getWindManual();
                break;
            case KNX_PATH_NAME_WIND0:
                data = dev.getWind0();
                break;
            case KNX_PATH_NAME_WIND1:
                data = dev.getWind1();
                break;
            case KNX_PATH_NAME_WIND2:
                data = dev.getWind2();
                break;
            case KNX_PATH_NAME_WIND3:
                data = dev.getWind3();
                break;
            case KNX_PATH_NAME_WIND4:
                data = dev.getWind4();
                break;
            case KNX_PATH_NAME_WIND5:
                data = dev.getWind5();
                break;
            case KNX_PATH_NAME_WIND6:
                data = dev.getWind6();
                break;
            case KNX_PATH_NAME_WIND7:
                data = dev.getWind7();
                break;
            case KNX_PATH_NAME_WIND_FB:
                data = dev.getWindFb();
                break;
            case KNX_PATH_NAME_MODE0:
                data = dev.getMode0();
                break;
            case KNX_PATH_NAME_MODE1:
                data = dev.getMode1();
                break;
            case KNX_PATH_NAME_MODE2:
                data = dev.getMode2();
                break;
            case KNX_PATH_NAME_MODE3:
                data = dev.getMode3();
                break;
            case KNX_PATH_NAME_MODE4:
                data = dev.getMode4();
                break;
            case KNX_PATH_NAME_MODE5:
                data = dev.getMode5();
                break;
            case KNX_PATH_NAME_MODE6:
                data = dev.getMode6();
                break;
            case KNX_PATH_NAME_MODE7:
                data = dev.getMode7();
                break;
            case KNX_PATH_NAME_MODE_FB:
                data = dev.getModeFb();
                break;
            default:
                data = 0;
                break;
        }
        return data;
    }


}
